package com.cheers.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.StaleObjectStateException;
import org.springframework.util.StringUtils;

import com.cheers.common.Constants;
import com.cheers.common.ResultListModel;
import com.cheers.common.ResultModel;
import com.cheers.model.LuckEvent;
import com.cheers.service.IntegralService;
import com.cheers.service.LuckEventService;
import com.cheers.util.VerifyUtil;


public class LuckEventAction extends BaseAction {

	private static final long serialVersionUID = -4647248677468251123L;
	
	@Resource
	private LuckEventService luckEventService;
	@Resource
	private IntegralService integralService;
	
	private String userId;
	private String luckId;
	private String pageIndex;
	private String number;
	private String cgyId;
	private String phase;
	
	
	/**
	 * 获取推荐
	 * @return
	 * @throws Exception
	 */
	public String recd() throws Exception {
		//获取推荐
		List<Map<String, Object>> list = luckEventService.recd();
		if (list!=null && !list.isEmpty()) {
			result = new ResultListModel(list, pageIndex, Constants.PAGESIZE, list.size());
		} else {
			result = new ResultListModel(new Object[0], pageIndex, Constants.PAGESIZE, 0);
		}
		return SUCCESS;
	}
	
	/**
	 * 查询活动详情
	 * @return
	 * @throws Exception
	 */
	public String detail() throws Exception {
		//验证
		if (StringUtils.isEmpty(cgyId)) {
			result = new ResultModel(Constants.NO, Constants.LUCKCGYID_DESC, "");
			return SUCCESS;
		}
		if (StringUtils.isEmpty(phase)) {
			phase = "1";
		}
		if (!VerifyUtil.numberTest(phase)) {
			result = new ResultModel(Constants.NO, Constants.LUCK_PHASENUM_DESC, "");
			return SUCCESS;
		}
		
		int phaseInt = Integer.parseInt(phase);
		//获取活动期数
		Map<String, String> data = luckEventService.load(cgyId, phaseInt);
		result = new ResultModel(data);
		return SUCCESS;
	}
	
	/**
	 * 活动期数列表
	 * @return
	 * @throws Exception
	 */
	public String phaseNum() throws Exception {
		//验证
		if (StringUtils.isEmpty(cgyId)) {
			result = new ResultModel(Constants.NO, Constants.LUCKCGYID_DESC, "");
			return SUCCESS;
		}
		
		//获取活动期数
		String data = luckEventService.phaseNum(cgyId);
		result = new ResultModel(data);
		return SUCCESS;
	}

	/**
	 * 活动详情
	 * @return
	 * @throws Exception
	 */
	public String load() throws Exception {
		//验证
		if (StringUtils.isEmpty(luckId)) {
			result = new ResultModel(Constants.NO, Constants.LUCKID_DESC, "");
			return SUCCESS;
		}
		
		//活动详情
		Map<String, Object> luck = luckEventService.load(luckId);
		if (luck != null) {
			result = new ResultModel(luck);
		} else {
			result = new ResultModel(null);
		}
		return SUCCESS;
	}
	
	/**
	 * 查找该期活动兑换列表
	 * @return
	 * @throws Exception
	 */
	public String find() throws Exception {
		//验证
		if (StringUtils.isEmpty(userId)) {
			result = new ResultModel(Constants.NO, Constants.USERID_DESC, "");
			return SUCCESS;
		}
		if (StringUtils.isEmpty(cgyId)) {
			result = new ResultModel(Constants.NO, Constants.LUCKCGYID_DESC, "");
			return SUCCESS;
		}
		if (StringUtils.isEmpty(phase)) {
			phase = "1";
		}
		if (!VerifyUtil.numberTest(phase)) {
			result = new ResultModel(Constants.NO, Constants.LUCK_PHASENUM_DESC, "");
			return SUCCESS;
		}
		if (StringUtils.isEmpty(pageIndex)) {
			pageIndex = "0";
		}
		if (!VerifyUtil.numberTest(pageIndex)) {
			result = new ResultModel(Constants.NO, Constants.PAGE_NUM_DESC, "");
			return SUCCESS;
		}
		
		int page = Integer.parseInt(pageIndex);
		int phaseInt = Integer.parseInt(phase);
		//查找该期活动兑换列表
		List<Map<String, Object>> list = luckEventService.find(userId, cgyId, phaseInt, page, Constants.PAGESIZE);
		if (list!=null && !list.isEmpty()) {
			result = new ResultListModel(list, pageIndex, Constants.PAGESIZE, list.size());
		} else {
			result = new ResultListModel(new Object[0], pageIndex, Constants.PAGESIZE, 0);
		}
		return SUCCESS;
	}
	
	/**
	 * 活动期数列表
	 * @return
	 * @throws Exception
	 */
	public String phases() throws Exception {
		//验证
		if (StringUtils.isEmpty(cgyId)) {
			result = new ResultModel(Constants.NO, Constants.LUCKCGYID_DESC, "");
			return SUCCESS;
		}
		if (StringUtils.isEmpty(pageIndex)) {
			pageIndex = "0";
		}
		if (!VerifyUtil.numberTest(pageIndex)) {
			result = new ResultModel(Constants.NO, Constants.PAGE_NUM_DESC, "");
			return SUCCESS;
		}
		
		int page = Integer.parseInt(pageIndex);
		//获取活动期数列表
		List<Map<String, Object>> list = luckEventService.phaseList(cgyId, page, Constants.PAGESIZE);
		if (list!=null && !list.isEmpty()) {
			result = new ResultListModel(list, pageIndex, Constants.PAGESIZE, list.size());
		} else {
			result = new ResultListModel(new Object[0], pageIndex, Constants.PAGESIZE, 0);
		}
		return SUCCESS;
	}
	
	/**
	 * 活动获奖者
	 * @return
	 * @throws Exception
	 */
	public String winner() throws Exception {
		//验证
		if (StringUtils.isEmpty(luckId)) {
			result = new ResultModel(Constants.NO, Constants.LUCKID_DESC, "");
			return SUCCESS;
		}
		
		//幸运活动获奖者
		Map<String, Object> data = luckEventService.winner(luckId);
		if (data == null) {
			result = new ResultModel(Constants.LOTTERY_CODE, Constants.LOTTERY_DESC, null);
			return SUCCESS;
		}
		//获奖者
		result = new ResultModel(Constants.OK, Constants.OK_DESC, data);
		return SUCCESS;
	}
	
	/**
	 * 我的活动幸运码
	 * @return
	 * @throws Exception
	 */
	public String codeList() throws Exception {
		//验证
		if (StringUtils.isEmpty(userId)) {
			result = new ResultModel(Constants.NO, Constants.USERID_DESC, "");
			return SUCCESS;
		}
		if (StringUtils.isEmpty(pageIndex)) {
			pageIndex = "0";
		}
		if (!VerifyUtil.numberTest(pageIndex)) {
			result = new ResultModel(Constants.NO, Constants.PAGE_NUM_DESC, "");
			return SUCCESS;
		}
		
		int page = Integer.parseInt(pageIndex);
		//我的幸运码列表
		List<Map<String, Object>> list = luckEventService.codeList(userId, page, Constants.PAGESIZE);
		if (list!=null && !list.isEmpty()) {
			result = new ResultListModel(list, pageIndex, Constants.PAGESIZE, list.size());
		} else {
			result = new ResultListModel(new Object[0], pageIndex, Constants.PAGESIZE, 0);
		}
		return SUCCESS;
	}
	
	/**
	 * 获奖者列表
	 * @return
	 * @throws Exception
	 */
	public String winnerList() throws Exception {
		if (StringUtils.isEmpty(pageIndex)) {
			pageIndex = "0";
		}
		if (!VerifyUtil.numberTest(pageIndex)) {
			result = new ResultModel(Constants.NO, Constants.PAGE_NUM_DESC, "");
			return SUCCESS;
		}
		
		int page = Integer.parseInt(pageIndex);
		//获取获奖者列表
		List<Map<String, Object>> list = luckEventService.winnerList(page, Constants.PAGESIZE);
		if (list!=null && !list.isEmpty()) {
			result = new ResultListModel(list, pageIndex, Constants.PAGESIZE, list.size());
		} else {
			result = new ResultListModel(new Object[0], pageIndex, Constants.PAGESIZE, 0);
		}
		return SUCCESS;
	}
	
	/**
	 * 幸运活动参加者列表
	 * @return
	 * @throws Exception
	 */
	public String joinList() throws Exception {
		//验证
		if (StringUtils.isEmpty(luckId)) {
			result = new ResultModel(Constants.NO, Constants.LUCKID_DESC, "");
			return SUCCESS;
		}
		if (StringUtils.isEmpty(pageIndex)) {
			pageIndex = "0";
		}
		if (!VerifyUtil.numberTest(pageIndex)) {
			result = new ResultModel(Constants.NO, Constants.PAGE_NUM_DESC, "");
			return SUCCESS;
		}
		
		int page = Integer.parseInt(pageIndex);
		//获取幸运活动参加者列表
		List<Map<String, Object>> list = luckEventService.joinList(luckId, page, Constants.PAGESIZE);
		if (list!=null && !list.isEmpty()) {
			result = new ResultListModel(list, pageIndex, Constants.PAGESIZE, list.size());
		} else {
			result = new ResultListModel(new Object[0], pageIndex, Constants.PAGESIZE, 0);
		}
		return SUCCESS;
	}
	
	/**
	 * 参加幸运活动
	 * @return
	 * @throws Exception
	 */
	public String luck() throws Exception {
		logger.info("number:"+number);
		//验证
		if (StringUtils.isEmpty(userId)) {
			result = new ResultModel(Constants.NO, Constants.USERID_DESC, "");
			return SUCCESS;
		}
		if (StringUtils.isEmpty(luckId)) {
			result = new ResultModel(Constants.NO, Constants.LUCKID_DESC, "");
			return SUCCESS;
		}
		if (!VerifyUtil.numberTest(number)) {
			result = new ResultModel(Constants.NO, Constants.LUCK_NUM_DESC, "");
			return SUCCESS;
		}
		if (!isPost("luck")) {
			result = new ResultModel(Constants.NO, Constants.INVALID_DESC, "");
			return SUCCESS;
		}
		
		int num = Integer.parseInt(number);
		//用积分兑换幸运活动
		String data = null;
		try {
			data = luckEventService.luck(userId, luckId, num);
		} catch (StaleObjectStateException e) {
			try {
				data = luckEventService.luck(userId, luckId, num);
			} catch (Exception e1) {
				result = new ResultModel(Constants.NO, Constants.NO_DESC, null);
				return SUCCESS;
			}
		}
		if (data == null) {
			result = new ResultModel(Constants.LUCK_CODE, Constants.LUCK_DESC, null);
			return SUCCESS;
		} else if(Constants.LUCK_NOT_CODE.equals(data)) {
			num = luckEventService.lastChance(luckId);
			result = new ResultModel(Constants.LUCK_NOT_CODE, Constants.LUCK_NOT_DESC+num, null);
			return SUCCESS;
		} else if (Constants.LUCK_FULL_CODE.equals(data)) {
			result = new ResultModel(Constants.LUCK_FULL_CODE, Constants.LUCK_FULL_DESC, null);
			return SUCCESS;
		} else if (Constants.USER_CODE.equals(data)) {
			result = new ResultModel(Constants.USER_CODE, Constants.USER_DESC, null);
			return SUCCESS;
		} else if (Constants.POINT_TOTAL_CODE.equals(data)) {
			result = new ResultModel(Constants.POINT_TOTAL_CODE, Constants.POINT_TOTAL_DESC, null);
			return SUCCESS;
		}
		//获取用户总积分
		Long total = integralService.getTotalPoint(userId);
		result = new ResultModel(Constants.OK, Constants.OK_DESC, String.valueOf(total));
		return SUCCESS;
	}
	
	/**
	 * 获取往期幸运列表
	 * @return
	 * @throws Exception
	 */
	public String phaseList() throws Exception {
		//验证
		if (StringUtils.isEmpty(pageIndex)) {
			pageIndex = "0";
		}
		if (!VerifyUtil.numberTest(pageIndex)) {
			result = new ResultModel(Constants.NO, Constants.PAGE_NUM_DESC, "");
			return SUCCESS;
		}
		
		int page = Integer.parseInt(pageIndex);
		//获取往期幸运列表
		List<Map<String, Object>> list = luckEventService.phaseList(page, Constants.PAGESIZE);
		if (list!=null && !list.isEmpty()) {
			result = new ResultListModel(list, pageIndex, Constants.PAGESIZE, list.size());
		} else {
			result = new ResultListModel(new Object[0], pageIndex, Constants.PAGESIZE, 0);
		}
		return SUCCESS;
	}
	
	/**
	 * 获取幸运广场列表
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		logger.info("userId:"+userId);
		//验证
		if (StringUtils.isEmpty(pageIndex)) {
			pageIndex = "0";
		}
		if (!VerifyUtil.numberTest(pageIndex)) {
			result = new ResultModel(Constants.NO, Constants.PAGE_NUM_DESC, "");
			return SUCCESS;
		}
		
		int page = Integer.parseInt(pageIndex);
		//获取幸运广场列表
		List<Map<String, Object>> list = luckEventService.list(page, Constants.PAGESIZE);
		if (list!=null && !list.isEmpty()) {
			result = new ResultListModel(list, pageIndex, Constants.PAGESIZE, list.size());
		} else {
			result = new ResultListModel(new Object[0], pageIndex, Constants.PAGESIZE, 0);
		}
		return SUCCESS;
	}
	
	/**
	 * 获取兑换记录列表
	 * @return
	 * @throws Exception
	 */
	public String exchangeList() throws Exception {
		//验证
		if (StringUtils.isEmpty(userId)) {
			result = new ResultModel(Constants.NO, Constants.USERID_DESC, "");
			return SUCCESS;
		}
		if (StringUtils.isEmpty(pageIndex)) {
			pageIndex = "0";
		}
		if (!VerifyUtil.numberTest(pageIndex)) {
			result = new ResultModel(Constants.NO, Constants.PAGE_NUM_DESC, "");
			return SUCCESS;
		}
		
		int page = Integer.parseInt(pageIndex);
		//获取兑换记录
		List<Map<String, Object>> list = luckEventService.exchangeList(userId, page, Constants.PAGESIZE);
		if (list!=null && !list.isEmpty()) {
			result = new ResultListModel(list, pageIndex, Constants.PAGESIZE, list.size());
		} else {
			result = new ResultListModel(new Object[0], pageIndex, Constants.PAGESIZE, 0);
		}
		return SUCCESS;
	}

	
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getLuckId() {
		return luckId;
	}
	public void setLuckId(String luckId) {
		this.luckId = luckId;
	}
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public String getPageIndex() {
		return pageIndex;
	}
	public void setPageIndex(String pageIndex) {
		this.pageIndex = pageIndex;
	}
	public String getCgyId() {
		return cgyId;
	}
	public void setCgyId(String cgyId) {
		this.cgyId = cgyId;
	}
	public String getPhase() {
		return phase;
	}
	public void setPhase(String phase) {
		this.phase = phase;
	}
	
}
